﻿namespace Manga.Api.CCTuku
{
    using System;
    using System.Threading.Tasks;
    using Core;
    using Core.Models;

    partial class CCGallery
    {
        private const string SingleSearchTemplate = "http://www.tukucc.com/search-{0}/";
        private const string MultiSearchTemplate = "http://www.tukucc.com/search/index/nickname/{0}/p/{1}.html";
        private const string SearchFilterTemplate = @"//div[@class='bookList_3']/div";
        private const string TotalPageTemplate = @"//div[@class='page']/a";
        private const string ComicUriTemplate = @".//div/a[1]";
        private const string ComicThumbnailTemplate = @".//div/a/img";
        private const string ComicTitleTemplate = @".//p[@class='title']/a";
        private const string ComicDescriptionTemplate = @".//p[@class='title']/a";
        async public Task<ISearchResult> Search(string key)
        {
            if (await Get(string.Format(SingleSearchTemplate, key)))
            {
                return Search(1);
            }
            return new SearchResult()
            {
                ContainResult = false,
                Result = null,
                Position = 0,
                Total = 0
            };
        }

        async public Task<ISearchResult> Search(string key, int nextPage)
        {
            if (await Get(string.Format(MultiSearchTemplate, key, nextPage)))
                return Search(nextPage);
            return new SearchResult()
            {
                ContainResult = false,
                Result = null,
                Position = 0,
                Total = 0
            };
        }
        private ISearchResult Search(int page)
        {
            try
            {
                var collection = Multi(SearchFilterTemplate);
                var pages = Multi(TotalPageTemplate);
                var last = pages[pages.Count - 1].Attributes["href"].Value;
                var total = string.IsNullOrEmpty(last)?"1":last.Substring(last.LastIndexOf('/'), last.LastIndexOf('.')-1);
                System.Diagnostics.Debug.WriteLine(total);
                var result = new SearchResult()
                {
                    ContainResult = true,
                    Position = page,
                    Result = new IComicModel[collection.Count]
                };
                if (int.TryParse(total, out var totalPage)) result.Total = totalPage;
                for (int i = 0; i < collection.Count; i++)
                {
                    var uri = HostUrl + Single(collection[i], ComicUriTemplate).Attributes["href"].Value;
                    var img = Single(collection[i], ComicThumbnailTemplate).Attributes["src"].Value;
                    var des = Single(collection[i], ComicDescriptionTemplate).InnerText;
                    var title = Single(collection[i], ComicTitleTemplate).InnerText;
                    result.Result[i] = new ComicModel()
                    {
                        Uri =uri,
                        Thumbnail = img,
                        Description = des,
                        Title =title,
                    };
                }
                return result;
            }
            catch
            {
                return new SearchResult()
                {
                    ContainResult = false,
                    Result = null,
                    Position = 0,
                    Total = 0
                };
            }
        }
    }
}
